package com.mop.app.controller.anymous;

import com.mop.app.common.exception.BizException;
import com.mop.app.common.util.FileHandleUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/anymous/file")
public class AnyFileController {

    @Value("${smas.captrue.image.path}")
    private String captureImagePath;

    @Value("${file.uploadFolder}")
    private String uploadFolder;

    @Value("${file.uri}")
    private String uri;


    @RequestMapping(value = "/upload")
    public List<String> emailUpload(HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> files = multipartRequest.getFileMap();
        String path_deposit = uploadFolder + captureImagePath;

        List<String> urls = new ArrayList<>();
        for (Map.Entry<String, MultipartFile> entry : files.entrySet()) {
            try {
                String fileName = entry.getValue().getOriginalFilename();

                fileName = FileHandleUtil.upload(entry.getValue().getInputStream(), path_deposit, fileName);
                urls.add(FileHandleUtil.getServerIPPort(request) + uri + fileName);
            } catch (IOException e) {
                throw new BizException("9090", "文件读取异常");
            }
        }

        return urls;
    }

}
